package com.oreilly.ez.fsm;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.*;

import java.util.EnumSet;

@Slf4j
@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {


    @Override
    public void configure(StateMachineConfigBuilder<OrderState, OrderEvent> config) throws Exception {
        super.configure(config);
    }

    @Override
    public void configure(StateMachineModelConfigurer<OrderState, OrderEvent> model) throws Exception {
        super.configure(model);
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderState, OrderEvent> config) throws Exception {
        super.configure(config);
    }

    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
        states.withStates().initial(OrderState.UNPAID).states(EnumSet.allOf(OrderState.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {

        transitions.
                withExternal().source(OrderState.UNPAID).target(OrderState.WAITING_FOR_RECEIVE).
                event(OrderEvent.PAY).
                and().withExternal().source(OrderState.WAITING_FOR_RECEIVE).target(OrderState.DONE).
                event(OrderEvent.RECEIVE);

    }
}
